package pers.rain.WebMagicDemo.Dao.impl;
import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import pers.rain.WebMagicDemo.Bean.Books;
import pers.rain.WebMagicDemo.Dao.BooksDao;
public class BooksDaoImpl implements BooksDao{

	@Override
	public int countAll() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Books queryBook(int id) {
		// TODO Auto-generated method stub
		String resource="mybatis_config.xml";
        Reader reader=null;
        SqlSessionFactory  ssf=null;
        SqlSession session=null;
        Books  book=null;
        try {
            reader = Resources.getResourceAsReader(resource);
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            ssf = builder.build(reader);
            session=ssf.openSession();
            BooksDao booksDao=session.getMapper(BooksDao.class);
            book=booksDao.queryBook(id);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            session.close();
        }
         
        return book;
	}

	@Override
	public int insertBook(Books book) {
		// TODO Auto-generated method stub
		String resource="mybatis_config.xml";
        Reader reader=null;
        SqlSessionFactory  ssf=null;
        SqlSession session=null;
        int count=0;
        try {
            reader = Resources.getResourceAsReader(resource);
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            ssf = builder.build(reader);
            session=ssf.openSession();
            count=session.insert("pers.rain.WebMagicDemo.Dao.BooksDao.insertBook",book);
            
            session.commit();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            session.close();
        }
        return count;
	}

	@Override
	public int deleteBook(int id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateBook(Books book) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Books> getAllBooks() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return super.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		return super.equals(obj);
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return super.toString();
	}

	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
	}

	
	
}
